<?php
/**
 * User: 五哥派
 * Date: 2024/7/1
 * Tel(同WX): 17335592719
 */
namespace app\api\model\store;

use app\api\model\Base;

class Goods extends Base{

    protected $name = 'store_goods';

    // 定义追加的字段
    protected $append = ['origin_image'];

    // 定义获取追加字段的方法
    public function getOriginImageAttr($value, $data)
    {
        return $data['image'];
    }

    public function getImageAttr($value){
        return $this->prefixImageUrl($value);
    }

    public function getCarouselImageAttr($value) {
        if(!empty($value)) {
            $value = explode(",", $value);

            $value = array_map(function($v){
                //return request()->domain() . $v;
                return config("setting.domain_prefix").config("setting.image_prefix").$v;
            }, $value);
        }
        return $value;
    }

    public function queryData($where=array(), $field = '', $order = ["id" => "desc", "create_time" => "desc"]){
        return self::field($field)->where($where)->order($order)->select();
    }

    public function  incStock($id, $num) {
        return $this->where("id", "=", $id)
            ->inc("stock", $num)
            ->update();
    }

}